<?
require_once('config.php');
$current_page = getValue('page','int','GET',1);
$limit_page   = 20;
$start        =   ($current_page -1) * $limit_page; 

/*------------- Xử lý tìm kiếm ------------------------*/
$action = getValue('action','str','POST','');
if($action == 'search'){
   $vid_name = getValue('vid_name','str','POST','');
   $date_from = getValue('date_from','str','POST',0);
   $date_from = str_replace('/','-',$date_from);
   $date_to = getValue('date_to','str','POST',0);
   $date_to = str_replace('/','-',$date_to);
   $start_date = strtotime('28-10-2013');
   $end_date = strtotime($date_to);
   $sql_select = "SELECT   
                        vid_id,
                        vid_id_youtube,
                        vid_name,
                        cat_id,
                        pla_id,
                        vid_lyric,
                        vid_duration,
                        vid_view,
                        vid_image,
                        vid_date,
                        vid_active,
                        vid_hot
                     FROM
                        video
                     WHERE 1 ";
   if($vid_name != ''){
      $sql_where = " AND vid_name LIKE'%" .$vid_name. "%'
                     ORDER BY vid_id DESC ";
   }else{
      $sql_where = " AND vid_date > " . $start_date . " AND vid_date < " . $end_date . " ORDER BY vid_id DESC";
   }
   $sql_search = $sql_select . $sql_where;
   echo $sql_search;
   $db_select  =  new db_query($sql_search);
   $array_vid  = $db_select->resultArray();
   $total_vid  = count($array_vid);
}else{
   $array_vid = $video->getListVideo($start,$limit_page);
   $total_vid = $video->getTotalVideo();
   $pages = ceil($total_vid/$limit_page);
}
/*----------------- Xử lý phân trang --------------------*/

$count_vid = count($array_vid); 



?>
<?=$css_global?>
<?=$js_global?>

<script>
   $(function() {
   
   $(document).on('click','.reload_tab' ,function(){
   window.location.reload();
   });
});
</script>
<div class="row-fluid">
   <div class="head">
      <div class="isw-grid"></div>
      <h1>Danh sách video</h1>
      <div class="reload_tab" title="Reload"></div>                              
      <div class="clear"></div>
   </div>                                
   <div class="block-fluid table-sorting">
      <div id="tSortable_wrapper" class="dataTables_wrapper" >
         <div class="dataTables_filter" id="tSortable_filter">
            <form method="POST" action="" style="margin: 0;">
               <table class="head-table">
                  <tr class="search">
                     <input type="hidden" name="action" id="action" value="search" />
                     <td><label>Tên video:</label></td>
                     <td><input id="vid_name" name="vid_name" width="200px" type="text" /></td>
                     <td><label>Từ ngày:</label></td>
                     <td><input class="datepicker" name="date_from" value="<?=date('d/m/Y', strtotime("-1 week"))?>" type="text" /></td>
                     <td><label>Đến ngày:</label></td>
                     <td><input class="datepicker" name="date_to" value="<?=date('d/m/Y', time())?>" type="text" /></td>
                     <td><input type="submit" class="bottom btn btn-info btn-small" value="Tìm kiếm" data-original-title="" title=""/></td>
                     <td>
                        
                     </td>
                  </tr>
                  <tr class="caption">
                     <td class="font12" colspan="2" style="text-align: left;"><b>Tổng số: <?=$total_vid?></b></td>
                     <td class="font12" colspan="2" style="text-align: left;"><b>Trang số: <?=$current_page?></b></td>
                     <td class="font12" colspan="2" style="text-align: left;"><b>Mỗi trang: <?=$limit_page?></b></td>
                  </tr>
               </table>
            </form>
         </div>
         <table cellpadding="0" cellspacing="0" width="100%" class="table dataTable table-bordered" id="tSortable"  style="width: 100%;">
            <thead>
              <tr>
                  <th class="sorting_disabled"  rowspan="1" colspan="1" style="width: 23px;">
                     <div class="checker">
                        <span>
                           <input type="checkbox" name="checkall" style="opacity: 0;"/>
                        </span>
                     </div>
                  </th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Sửa</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Xóa</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">ID</th>
                  <th width="15%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Tên video</th>
                  <th width="18%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Ca sĩ,Link video</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Thể loại</th>
                  <th width="10%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Playlist</th>
                  <th width="12%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Ngày tạo</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Thời lượng</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">View</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Hot</th>
                  <th width="5%" class="sorting" tabindex="0" rowspan="1" colspan="1" style="">Active</th>

                  
               </tr>
            </thead>
              <?
              if($count_vid > 0){
                  foreach($array_vid as $value){
                  /*--------- Lấy tên ca sĩ của video ---------*/
                  $array_per = array();
                  $db_query = new db_query($sql = " SELECT 
                                                person.per_id,per_name 
                                             FROM 
                                                person
                                             INNER JOIN
                                                vid_per
                                             ON
                                                person.per_id = vid_per.per_id
                                             WHERE vid_id=" . $value['vid_id']);
                                             //echo $sql;
                  $array_per = $db_query->resultArray();
                  $per_name = '';
                  $array_json_per = array();
                  foreach($array_per as $values){
                     $per_name.= $values['per_name'].',';
                     $array_json_per[] = array( 'id'=>$values['per_id'],
                                                'name'=>$values['per_name']);
                  }   
                  ?>
                     <tr class="even" data-vid=<?=$value['vid_id']?>>
                        <td class="font12 sorting_1">
                           <div class="checker check-to-delete">
                              <span>
                                 <input type="checkbox" class="checkbox-row" name="row_to_delete[]" value="<?=$value['vid_id']?>" style="opacity: 0;"/>
                              </span>
                           </div>
                        </td>
                        <td class="font12">
                           <a href="edit.php?id=<?=$value['vid_id']?>">
                              <span class="edit-row">Sửa</span>
                           </a>
                        </td>
                        <td class="font13">
                           <span class="delete-row" onclick="deleteOnlyRow(this)" data-id=<?=$value['vid_id']?>  >Xóa</span>
                        </td>
                        <td class="font12"><?=$value['vid_id']?></td>
                        <td class="font12">
                           <span class="vid-name"><?=$value['vid_name']?></span>
                           <textarea data-id="<?=$value['vid_id']?>" class="edit-vid-name hide" onblur="editQuickVidName(this)"><?=$value['vid_name']?></textarea>
                           <p><a href="javascript:void(0)" onclick="showEditVidName(this)">Sửa tên</a></p>
                        </td>   
                        <td class="font12">
                           <div style="margin: 5px 0px;">
                              <span>Xem trước</span>
                              <a target="_blank" href="http://www.youtube.com/watch?v=<?=$value['vid_id_youtube']?>">
                              <img width="100" src="<?=$value['vid_image']?>" />
                              </a>
                           </div>
                           <input type="text" id="vid-per-<?=$value['vid_id']?>" class="per_id span5 vid-name" name="per_id" />
                           <script>
                              $(document).ready(function(){
                                 $("#vid-per-<?=$value['vid_id']?>").tokenInput("ajax_token_input_person.php", {
                                    theme: "facebook",
                                    preventDuplicates: true,
                                    onAdd:function(item){
                                      addPerson(<?=$value['vid_id']?>,item.id);
                                    },
                                    onDelete:function(item){
                                       deletePerson(<?=$value['vid_id']?>,item.id);
                                    },
                                    prePopulate: [
                                         <?foreach($array_json_per as $person){?>
                                          {id:<?=$person['id']?>,name:'<?=$person['name']?>'},
                                         <?}?>
                                        
                                     ],
                                    
                                 });
                              });
                           </script>
                           
                        </td>
                               
                        <td class="font12">
                           <?= isset($array_all_cat[$value['cat_id']]) ? $array_all_cat[$value['cat_id']]['cat_name']: 'Chưa rõ' ?>
                        </td>
                        <td class="font12">
                           <?= isset($array_all_pla[$value['pla_id']]) ?  $array_all_pla[$value['pla_id']]['pla_name']: 'Chưa rõ' ?>
                        </td>
                              
                        <td class="font12"><?=($value['vid_date'] > 0) ? date('H:m - d/m/Y', $value['vid_date']) : 'Chưa rõ'?></td>      
                        <td class="font12"><?=$value['vid_duration']?></td>      
                        <td class="font12"><?=$value['vid_view']?></td>
                        <td class="font12 sorting_1" onclick="checkHot(this)">
                           <div class="checker">
                              <span class="<?if($value['vid_hot'] == 1) echo 'checked';?>">
                                 <input type="checkbox" class="active-row" data-id=<?=$value['vid_id']?>  value="<?=$value['vid_hot']?>" />
                              </span>
                           </div>
                        </td> 
                        <td class="font12 sorting_1" onclick="activeRow(this)">
                           <div class="checker">
                              <span class="<?if($value['vid_active'] == 1) echo 'checked';?>">
                                 <input type="checkbox" class="active-row" data-id=<?=$value['vid_id']?>  value="<?=$value['vid_active']?>" />
                              </span>
                           </div>
                        </td>                      
                    </tr>
                  <?}?>
              <?}?>
          </table>
         </div>
      <div class="clear"></div>
      <div class="mult_submit pull-left">
         <span>Với những dòng đã chọn: </span>  <span class="delete-row mult-delete">Xóa</span> 
      </div>
      <?if($action != 'search'):?>
      <div class="paging pull-right">
         <?php $pagination = pagination($current_page,$pages,'list.php?page=%d') ?>
         <ul>
         <?php foreach ($pagination as $link): ?>
           <li><?php echo $link ?></li>
         <?php endforeach ?>
         </ul>
      </div>
      <?endif?>
   </div>
</div>
<script>
$(function(){
   
   $('.mult-delete').click(function(){
      var matches = [];
      $(".checkbox-row:checked").each(function() {
          matches.push(this.value);
          string_cat_id = matches.join(',');
      });
      data = {'id':string_cat_id};
      var updateHTML = function(data){
         if(data != ''){
            alert(data);
            window.location.reload();
         }
      }
      var beforeSend = function(){
         
      }
      var conf = confirm("Xóa những dòng đã chọn ?");
      if(conf)
         handleAjax('delete.php','POST',data,updateHTML,beforeSend);
   });
   $('.vid-name').mouseup(function(e){
      var vid = $(this).parent('tr.even').attr('data-vid');
      var text = getSelectedText();
      if($.trim(text) != ''){
         quickInsertPerson(vid,text,e.pageX,e.pageY);   
      }else{
         $('.quick-insert-person').hide(); 
      }
      
      
   });
});
</script>